﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LED_Tester
{
    public partial class frmRMeasSetting : Form
    {
        public frmRMeasSetting()
        {
            InitializeComponent();
        }

        #region 变量声明

        private ClassSpectralMeter.Param param;

        private double voltSet;
        private double currMax;
        private double currMin;
        private double currSpace;
        private int timeSpace;

        private bool isR6 = false;
        private bool isR7 = false;

        public ClassSpectralMeter.Param Param
        {
            get { return param; }
            set { param = value; }
        }
        public double VoltSet
        {
            get { return voltSet; }
            set { voltSet = value; }
        }
        public double CurrSpace
        {
            get { return currSpace; }
            set { currSpace = value; }
        }
        public double CurrMax
        {
            get { return currMax; }
            set { currMax = value; }
        }
        public double CurrMin
        {
            get { return currMin; }
            set { currMin = value; }
        }
        public int TimeSpace
        {
            get { return timeSpace; }
            set { timeSpace = value; }
        }
        public bool IsR6
        {
            get { return isR6; }
            set { isR6 = value; }
        }
        public bool IsR7
        {
            get { return isR7; }
            set { isR7 = value; }
        }

        #endregion

        #region 窗体事件

        private void frmRMeasSetting_Load(object sender, EventArgs e)
        {
            if (param.IsAutoAdjust)
            {
                rdoAutoAdjust.Checked = true;
                rdoManualAdjust.Checked = false;
                nudIntTime.Enabled = false;
            }
            else
            {
                rdoAutoAdjust.Checked = false;
                rdoManualAdjust.Checked = true;
                nudIntTime.Enabled = true;
            }

            nudIntTime.Value = param.IntTime;

            nudTimeSpace.Value = timeSpace;

            txtVoltSet.Text = voltSet.ToString("#0.0000");
            txtCurrMax.Text = currMax.ToString("#0.0000");
            txtCurrMin.Text = currMin.ToString("#0.0000");
            txtCurrSpace.Text = currSpace.ToString("#0.0000");

            if (isR6)
            {
                lblCurrSet.Text = "电    流(A)";
                lblCurrMin.Visible = false;
                lblCurrSpace.Visible = false;
                txtCurrMin.Visible = false;
                txtCurrSpace.Visible = false;
            }
            else if (isR7)
            {
                lblTimeSpace.Visible = false;
                nudTimeSpace.Visible = false;

                lblCurrSet.Text = "电    流(A)";
                lblCurrMin.Visible = false;
                lblCurrSpace.Visible = false;
                txtCurrMin.Visible = false;
                txtCurrSpace.Visible = false;
            }
        }

        private void frmRMeasSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (rdoAutoAdjust.Checked)
            {
                param.IsAutoAdjust = true;
            }
            else
            {
                param.IsAutoAdjust = false;
            }

            param.IntTime = (int)nudIntTime.Value;

            timeSpace = (int)nudTimeSpace.Value;

            voltSet = double.Parse(txtVoltSet.Text);
            currMax = double.Parse(txtCurrMax.Text);
            currMin = double.Parse(txtCurrMin.Text);
            currSpace = double.Parse(txtCurrSpace.Text);
        }

        #endregion

        #region 功能函数

        private static void TxtDataVerifyWithOnePoint(KeyPressEventArgs e, TextBox txt)
        {
            int pointCount = 0;
            bool isPointExist = false;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
                if (txt.Text.Length == 0)
                {
                    if (e.KeyChar == '.')
                        e.Handled = true;
                }

                for (int i = 0; i < txt.Text.Length; i++)
                {
                    if (txt.Text.Substring(i, 1) == ".")
                        pointCount++;
                    if (pointCount > 0)
                        isPointExist = true;
                }

                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }

            else { e.Handled = true; }
        }

        #endregion

        #region 按钮事件

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        #endregion

        #region 其它事件

        private void rdoAutoAdjust_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoAutoAdjust.Checked && !rdoManualAdjust.Checked)
            {
                nudIntTime.Enabled = false;
            }
            else if (!rdoAutoAdjust.Checked && rdoManualAdjust.Checked)
            {
                nudIntTime.Enabled = true;
            }
        }

        private void rdoManualAdjust_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoAutoAdjust.Checked && !rdoManualAdjust.Checked)
            {
                nudIntTime.Enabled = false;
            }
            else if (!rdoAutoAdjust.Checked && rdoManualAdjust.Checked)
            {
                nudIntTime.Enabled = true;
            }
        }

        private void txtVoltSet_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtVoltSet);
        }

        private void txtCurrMax_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtCurrMax);
        }

        private void txtCurrMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtCurrMin);
        }

        private void txtCurrSpace_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtCurrSpace);
        }

        #endregion
    }
}
